package com.example.lending.View;


import android.content.Context;
import android.os.Bundle;

import androidx.annotation.Nullable;
import androidx.fragment.app.Fragment;
import androidx.lifecycle.LiveData;
import androidx.lifecycle.Observer;
import androidx.lifecycle.SavedStateViewModelFactory;
import androidx.lifecycle.ViewModelProviders;
import androidx.navigation.NavController;
import androidx.navigation.Navigation;

import android.text.TextUtils;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.Button;
import android.widget.EditText;
import android.widget.Toast;

import com.example.lending.Model.User;
import com.example.lending.R;
import com.example.lending.ViewModel.UserViewModel;

import java.util.List;

/**
 * A simple {@link Fragment} subclass.
 */
public class UpdataUserFragment extends Fragment {

    EditText et_updata_name,et_updata_password,et_updata_age,et_updata_class,et_updata_email,et_updata_typde;
    Button button;
    UserViewModel userViewModel;
    public UpdataUserFragment() {
        // Required empty public constructor
    }
    public User getUser(String name ,String password,int age,String classs,String email,int type){
        User user=new User();
        user.setEmail(email);
        user.setPassword(password);
        user.setName(name);
        user.setType(type);
        user.setAge(age);
        user.setClassnum(classs);
        return user;
    }
    public void inteView(){
        et_updata_name=getView().findViewById(R.id.et_updatauser_name);
        et_updata_age=getView().findViewById(R.id.et_updatauser_age);
        et_updata_password=getView().findViewById(R.id.et_updatauser_password);
        et_updata_class=getView().findViewById(R.id.et_updatauser_class);
        et_updata_email=getView().findViewById(R.id.et_updatauser_email);
        et_updata_typde=getView().findViewById(R.id.et_updatauser_type);
        button=getView().findViewById(R.id.btn_user_update);
        userViewModel= ViewModelProviders.of(getActivity(),new SavedStateViewModelFactory(getActivity().getApplication(),getActivity())).get(UserViewModel.class);
    }
    String name,email,passsword,classs;
    int age=-1,type=-1;
    @Override
    public void onActivityCreated(@Nullable Bundle savedInstanceState) {
        super.onActivityCreated(savedInstanceState);
        inteView();
        String username=getActivity().getSharedPreferences("user", Context.MODE_PRIVATE).getString("username","");
        final int leveluser=getActivity().getSharedPreferences("user",Context.MODE_PRIVATE).getInt("usertype",-1);
            button.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(final View v) {
                    name=et_updata_name.getText().toString();
                    email=et_updata_email.getText().toString();
                    passsword=et_updata_password.getText().toString();
                    classs=et_updata_class.getText().toString();
                    if(!et_updata_age.getText().toString().isEmpty())
                        age=Integer.parseInt(et_updata_age.getText().toString());
                    if(!et_updata_typde.getText().toString().isEmpty())
                        type=Integer.parseInt(et_updata_typde.getText().toString());
                    if(TextUtils.isEmpty(name)||TextUtils.isEmpty(email)||TextUtils.isEmpty(passsword)||TextUtils.isEmpty(et_updata_age.getText().toString())||TextUtils.isEmpty(et_updata_typde.getText().toString())){
                        Toast.makeText(getActivity(), "请将数据补充完毕", Toast.LENGTH_SHORT).show();

                    }else {
                    if(leveluser==100){
                        Toast.makeText(getActivity(), "非管理员不可更改用户信息", Toast.LENGTH_SHORT).show();
                    }else {
                        User user = new User();
                        user = getUser(name, passsword, age, classs, email, type);
                        String getname = getActivity().getSharedPreferences("adapter", Context.MODE_PRIVATE).getString("username", "");
                        LiveData<List<User>> liveUser = userViewModel.findUserWithPattern(getname);
                        liveUser.observe(getViewLifecycleOwner(), new Observer<List<User>>() {
                            @Override
                            public void onChanged(List<User> users) {
                                //for (int i = 0; i <= users.size(); i++) {
                                    //userViewModel.deleteUsers(users.get(i));
                                    //userViewModel.insertUsers(user);
                                int i=0;
                                    users.get(i).setEmail(email);
                                    users.get(i).setPassword(passsword);
                                    users.get(i).setName(name);
                                    users.get(i).setType(type);
                                    users.get(i).setAge(age);
                                    users.get(i).setClassnum(classs);
                                    userViewModel.updataUsers(users.get(i));
                                    Toast.makeText(getActivity(), "修改成功", Toast.LENGTH_SHORT).show();
                                //}
                                //Toast.makeText(getActivity(), "修改失败", Toast.LENGTH_SHORT).show();
                                NavController controller = Navigation.findNavController(v);
                                controller.navigate(R.id.action_updataUserFragment2_to_homeFragment);
                            }
                        });
                    }
                        //List<User>users= liveUser.getValue();

                    }
                }
            });



    }

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container,
                             Bundle savedInstanceState) {
        // Inflate the layout for this fragment
        return inflater.inflate(R.layout.fragment_updata_user, container, false);
    }

}
